// m

// 给定一个由0,1组成的数组nums，再给定一个整数k，最多可以将k个值从0变到1
// 返回仅包含1的最长连续子数组的长度

// 思路：滑动窗口（不定长度）
function longestOnes(nums, k) {
    let max_count = 0
    let zero_count = 0
    let left = 0, right = 0
    while (right < nums.length) {
        if (nums[right] === 0) {
            zero_count += 1
        }
        right += 1
        if (zero_count > k) {
            if (nums[left] === 0) {
                zero_count -= 1
            }
            left += 1
        }
        max_count = Math.max(max_count, right - left)
    }
    return max_count
}

let nums = [1,1,1,0,0,0,1,1,1,1,0], K = 2
console.log(longestOnes(nums, K))